﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    [ServiceContract(Namespace = "")]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class CostCenterServices
    {
        private readonly CostCenterRepository CostCenterRepository;

        public CostCenterServices()
        {
            CostCenterRepository = new CostCenterRepository(new WcfSessionStorage());
        }


        [OperationContract]
        public List<CostCenterDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                           string sortType = "ASC")
        {
            var CostCenters = new List<CostCenter>();
            var Results = new List<CostCenterDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                CostCenters = CostCenterRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                CostCenters = CostCenterRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = CostCenterRepository.RecordCount;

            CostCenters.ForEach(x =>
                                    {
                                        var CostCenterDto = new CostCenterDTO();
                                        Mapper.Map(x, CostCenterDto);
                                        //AutoMapper.Mapper.Map(x.ParentCostCenter, CostCenterDto.ParentCostCenter);
                                        Results.Add(CostCenterDto);
                                    }
                );

            return Results;
        }

        [OperationContract]
        public bool Save(CostCenterDTO CostCenterDTO)
        {
            bool res;
            try
            {
                CostCenter CostCenter =
                    CostCenterRepository.GetDataOne(x => x.CostCenterId == CostCenterDTO.CostCenterId);
                var user = new User();
                if (CostCenter == null)
                {
                    CostCenter = new CostCenter();
                }
                Mapper.Map(CostCenterDTO, CostCenter);
                CostCenterRepository.SaveOrUpdate(CostCenter);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }

        [OperationContract]
        public bool Delete(CostCenterDTO CostCenterDto)
        {
            bool res;
            try
            {
                var CostCenter = new CostCenter();
                Mapper.Map(CostCenterDto, CostCenter);
                CostCenterRepository.Delete(CostCenter);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }

        [OperationContract]
        public List<CostCenterDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                     out long recordCount)
        {
            var CostCenters = new List<CostCenter>();
            var Results = new List<CostCenterDTO>();
            CostCenters = CostCenterRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = CostCenterRepository.RecordCount;
            CostCenters.ForEach(x =>
                                    {
                                        var CostCenterDto = new CostCenterDTO();
                                        Mapper.Map(x, CostCenterDto);
                                        Results.Add(CostCenterDto);
                                    }
                );

            return Results;
        }

        [OperationContract]
        public List<CostCenterDTO> GetAllCostCenter()
        {
            List<CostCenter> CostCenters = CostCenterRepository.FetchAll().ToList();
            var Results = new List<CostCenterDTO>();
            CostCenters.ForEach(x =>
                                    {
                                        var CostCenterDto = new CostCenterDTO();
                                        Mapper.Map(x, CostCenterDto);
                                        Results.Add(CostCenterDto);
                                    }
                );

            return Results;
        }
    }
}